/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.LocalDao;
import edu.upc.clase.demo.entity.Local;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author joelpacheco
 */
@Repository
public class LocalDaoImpl extends SimpleJdbcDaoSupport implements LocalDao {

    private static Logger log = LoggerFactory.getLogger(LocalDaoImpl.class);

    @Autowired
    public LocalDaoImpl(DataSource datasource) {
        log.info("Asignando DataSource");
        setDataSource(datasource);

    }

    @Override
    public Integer insertar(Local local) {
        getJdbcTemplate().update(
                "INSERT INTO Local(\n"
                + " nombre,\n"
                + " direccion,\n"
                + " horario,\n"
                + " borrado,\n"
                + " usuarioId)\n"
                + "VALUES\n"
                + "(\n"
                + "?,\n"
                + "?,\n"
                + "?,\n"
                + "?,\n"
                + "?\n"
                + ")",
                local.getNombre(), local.getDireccion(), local.getHorario(), local.getBorrado(), local.getUsuario().getUsuarioId());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }

    @Override
    public void actualizar(Local local) {
        getJdbcTemplate().update(
                "UPDATE Local\n"
                + "SET\n"
                + " nombre = ?,\n"
                + " direccion = ?,\n"
                + " horario = ?,\n"
                + " borrado = ?\n"
//                + " usuarioId = ?\n"
                + " WHERE localId = ?;",
                local.getNombre(), local.getDireccion(), local.getHorario(), local.getBorrado(), local.getLocalId());
    }

    @Override
    public void eliminar(Local local) {
        getJdbcTemplate().update(
                "DELETE FROM Local\n"
                + " WHERE localId = ?;", local.getLocalId());
    }

    @Override
    public List<Local> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "SELECT\n"
                + "localId,\n"
                + "nombre,\n"
                + "direccion,\n"
                + "horario,\n"
                + "borrado,\n"
                + "usuarioId\n"
                + "FROM Local",
                new BeanPropertyRowMapper<Local>(Local.class));
    }

    @Override
    public Local buscar(Integer id) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "SELECT\n"
                    + " localId,\n"
                    + " nombre,\n"
                    + " direccion,\n"
                    + " horario,\n"
                    + " usuarioId,\n"
                    + " borrado\n"
                    + " FROM Local WHERE localId = ?",
                    new BeanPropertyRowMapper<Local>(Local.class), id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Local> buscarPorNombre(String nombre) {
        try {
            Map<String, String> parametros = new HashMap<String, String>();
            parametros.put("nombres", "%" + nombre + "%");
            return getSimpleJdbcTemplate().query(
                    "select * from Local WHERE nombre like :nombre",
                    new BeanPropertyRowMapper<Local>(Local.class), parametros);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
